package com.yingsheng.bi.hks.template.events;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

public class SimpleEvent implements Event {

    private Map<String, Object> dataMap;

    public SimpleEvent() {

    }

    public SimpleEvent(Map<String, Object> initMap) {
        if (initMap == null) {
            initMap = new HashMap<>();
        }
        this.dataMap = initMap;
    }

    @Override
    public String getId() {
        return null;
    }

    @Override
    public Object getData(String key) {
        Object object = null;
        Map<String, Object> dataMap = getDataMap();
        if ((object = dataMap.get(key)) == null) {
            String convertedName = Character.toUpperCase(key.charAt(0)) + key.substring(1);
            object = dataMap.get(convertedName);
        }
        return object;
    }

    @Override
    public void setData(String key, Object value) {
        getDataMap().put(key, value);
    }

    public String getString(String key) {
        Object object = getData(key);
        if (object == null) {
            return "";
        } else {
            return object instanceof String ? (String) object : object.toString();
        }
    }

    public BigDecimal getBigDecimal(String key) {
        Object object = getData(key);
        if (object == null) {
            return null;
        } else if (object instanceof BigDecimal) {
            return (BigDecimal) object;
        } else if (object instanceof String) {
            String tmpStr = object.toString().trim();
            return tmpStr.length() == 0 ? null : new BigDecimal(tmpStr);
        } else if (object instanceof Number) {
            return new BigDecimal(object.toString());
        } else {
            throw new RuntimeException("atb.unsupported_convert");
        }
    }

    public Boolean getBoolean(String key) {
        Object object = getData(key);
        if (object == null) {
            return null;
        } else if (object instanceof Boolean) {
            return (Boolean) object;
        } else if (object instanceof String) {
            if (((String) object).length() == 0) {
                return null;
            } else if (((String) object).length() == 1) {
                char ch = ((String) object).charAt(0);
                ch = Character.toUpperCase(ch);
                switch (ch) {
                    case '0':
                    case 'F':
                    case 'N':
                        return Boolean.FALSE;
                    case '1':
                    case 'T':
                    case 'Y':
                        return Boolean.TRUE;
                    default:
                        throw new RuntimeException("atb.unsupported_boolean");
                }
            } else {
                return new Boolean((String) object);
            }
        } else {
            throw new RuntimeException("atb.unsupported_boolean");
        }
    }

    @Override
    public Map<String, Object> getDataMap() {
        if (this.dataMap == null) {
            this.dataMap = new HashMap<>();
        }
        return this.dataMap;
    }

    @Override
    public void setDataMap(Map<String, Object> dataMap) {
        this.dataMap = dataMap;
    }

    @Override
    public void putAll(Map<String, Object> dataMap) {
        if (!CollectionUtils.isEmpty(dataMap)) {
            getDataMap().putAll(dataMap);
        }
    }

    @Override
    public void reset() {

    }
}
